﻿package window
{
	import flash.desktop.NativeApplication;
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.NativeWindowResize;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.NativeWindowType;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import skinfactory.SkinImage;
	import skinfactory.SkinScaleImage;
	
	public class CustomNativeWindow extends NativeWindow
	{
		
		private var resizer: Sprite;
		
		private var list_TitleBG: SkinScaleImage;
		private var list_BG: SkinScaleImage;
		private var list_Title: SkinImage;
		
		public function CustomNativeWindow()
		{
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			
			var options: NativeWindowInitOptions = new NativeWindowInitOptions();			
			options.systemChrome = NativeWindowSystemChrome.NONE;
			options.type = NativeWindowType.UTILITY;
			options.transparent = true;			
			super ( options );
			
			list_BG = new SkinScaleImage ( "window_bg" );	
			drawBackground ( 200, 200 );
			this.stage.addChild( list_BG );
			
			list_TitleBG = new SkinScaleImage ( "list_topbg" );			
			list_TitleBG.x = 3;
			list_TitleBG.width = stage.stageWidth - 3;
			list_TitleBG.height = 21;
			this.stage.addChild( list_TitleBG );
			
			list_Title = new SkinImage ( "list_title" );
			list_Title.x = 3;
			list_Title.y = 4;
			this.stage.addChild( list_Title );
			
			// 리사이즈를 위한 객체
			resizer = new Sprite();
			resizer.graphics.beginFill( 0xff3399 );
			resizer.graphics.drawRect( 0,0, 10, 10 );
			resizer.graphics.endFill();
			resizer.x = this.width - 50;
			resizer.y = this.height - 50;
			this.stage.addChild( resizer );						
			
			// 전체 닫기를 위한 Evnet
			this.addEventListener( Event.CLOSING, closeHandler );			
									
			// StartMove		
			list_TitleBG.addEventListener( MouseEvent.MOUSE_DOWN, startWindowHandler );
			
			// resizing Handler
			this.addEventListener( Event.RESIZE, resizingHandler );
			resizer.addEventListener( MouseEvent.MOUSE_DOWN, startResizingHandler );
			
			// 최상단으로 올리기위한 Event
			this.addEventListener( Event.ACTIVATE, activateHandler );			
								
		}
		
		
		private function drawBackground ( w:uint, h:uint ): void
		{			
			list_BG.width = this.stage.width;
			list_BG.height = this.stage.height;		
		}
		
		private function startResizingHandler ( e: MouseEvent ): void
		{ 
			startResize( NativeWindowResize.BOTTOM_RIGHT );
				
		}
		
		private function resizingHandler ( e: Event ): void
		{
			drawBackground ( this.width, this.height );
			resizer.x = this.width - 10;
			resizer.y = this.height - 10;			
		}
		
		private function startWindowHandler ( e: MouseEvent ): void
		{
			this.startMove();
		}
		
		private function closeHandler ( e: Event ): void
		{
			var windowsMember: Array = NativeApplication.nativeApplication.openedWindows;
			var n: uint = windowsMember.length;
			while ( n-- )
			{
				windowsMember[ n ].close();
			}
		}
		
		private function activateHandler ( e: Event ): void
		{
			resizer.x = this.width - 10;
			resizer.y = this.height - 10;
		}

	}
}